package com.xtaller.easy.admin.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.xtaller.common.convert.F;
import com.xtaller.common.convert.J;
import com.xtaller.easy.admin.async.CacheSysTask;
import com.xtaller.easy.admin.base.TServiceImpl;
import com.xtaller.easy.admin.mapper.SysOrganizationalMapper;
import com.xtaller.easy.admin.model.SysOrganizational;
import com.xtaller.easy.admin.service.ISysOrganizationalService;
import com.xtaller.easy.admin.tps.CacheKit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import com.xtaller.common.convert.W;
import com.xtaller.common.bean.Where;
/**
* Created by Taller on 2018/06/02
*/
@Service
public class SysOrganizationalService extends TServiceImpl<SysOrganizationalMapper,SysOrganizational> implements ISysOrganizationalService {
    @Autowired
    private CacheKit cacheKit;
    @Autowired
    @Lazy
    private CacheSysTask cacheSysTask;

    private static final String CACHE_ORGANIZATIONAL = "cache-sys-organizational";
    /**************************CURD begin******************************/
   // 创建
   @Override
   public SysOrganizational createModel(SysOrganizational model) {
       if(this.insert(model)) {
           cacheSysTask.refurbishData();
           return this.selectById(model.getId());
       }
       return null;
   }
 
   // 删除
   @Override
   public Boolean deleteModel(Object ids,String reviser) {
       if(this.delete(ids,reviser)){
           cacheSysTask.refurbishData();
           return true;
       }else {
           return false;
       }
   }
 
   // 修改
   @Override
   public SysOrganizational updateModel(SysOrganizational model) {
       if(this.update(model)) {
           cacheSysTask.refurbishData();
           return this.selectById(model.getId());
       }
       return null;
   }
 
   // 查询
   @Override
   public List<SysOrganizational> findByIds(Object ids) {
       return this.selectByIds(ids);
   }
 
   // 属于
   @Override
   public Boolean exist(List<Where> w) {
       w.add(new Where("1"));
       return this.query(w).size() > 0;
   }
 
   // 查询一个id是否存在
   @Override
   public Boolean existId(Object id) {
       where = W.f(
               W.and("id","eq",id),
               W.field("1")
       );
       return this.query(where).size() > 0;
   }
 
   // 获取所有
   @Override
   public List<SysOrganizational> getData() {
       List<SysOrganizational> organizationals = new ArrayList<>();
       try{
           List<JSONObject> list = cacheKit.getList(CACHE_ORGANIZATIONAL);
           if(list == null){
               organizationals = getList();
               cacheKit.setVal(CACHE_ORGANIZATIONAL, organizationals);
           }else {
               organizationals = J.o2l(list, SysOrganizational.class);
           }
       }catch (Exception ex){
           organizationals = getList();
           cacheKit.setVal(CACHE_ORGANIZATIONAL, organizationals);
       }
       return organizationals;
   }

    /**************************CURD end********************************/
    @Override
    public Boolean checkParentId(String parentId) {
        where = W.f(
                W.and("parentId", "eq", parentId)
        );
        return this.query(where).size() > 0;
    }

    @Override
    public List<JSONObject> getByKey(JSONObject object) {
        return F.f2l(baseMapper.getByKey(object));
    }

    private List<SysOrganizational> getList(){
        where = W.f(
                W.field("id", "name", "parentId", "sort"),
                W.and(1,"eq",1),
                W.order("sort", "order", "asc")
        );
        return this.query(where);
    }

    // 更新缓存数据
    public void refurbishData(){
        cacheKit.setVal(CACHE_ORGANIZATIONAL, getList());
    }
}